package me.yobol.blog.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import me.yobol.blog.bean.UserBean;
import me.yobol.blog.dao.IUserDAO;

public class UserDAOImpl implements IUserDAO {

	private String sql;
	private String tableName = "tb_yobol_user";
	//idFN = user_id Field Name
	private String idFN = "user_id",
				   nameFN = "name",
				   passwordFN = "password",
				   emailFN = "email",
				   genderFN = "gender",
				   birthdayFN = "birthday",
				   phoneFN = "phone",
				   registerTimeFN = "register_time",
				   latestLoginTimeFN = "latest_login_time";
	private PreparedStatement ps;
	private ResultSet rs;
	

	
	/**
	 * 在用户注册的时候使用，只用到nameFN
	 * @param ctn
	 * @param user
	 * @return 当成功插入用户记录后，返回记录主键；否则返回-1
	 * @throws SQLException
	 */
	@Override
	public int save(Connection ctn,UserBean user) throws SQLException {
		sql = "insert into " + tableName + "("
				+ nameFN + ","
				+ passwordFN
				+ ") values(?,?);";
		ps = ctn.prepareCall(sql);
		ps.setString(1, user.getName());
		ps.setString(2, user.getPassword());
		ps.execute();
		
		rs = ps.getGeneratedKeys();
		if(rs.next()){
			return rs.getInt(1);
		}
		
		return -1;
	}

	@Override
	public UserBean delete(Connection ctn,int userId) throws SQLException {
		//获取到要删除的用户记录，并封装到UserBean中
		UserBean user = get(ctn, userId);

		sql = "delete from " + tableName + " where " + idFN + " = " + userId;
		ps = ctn.prepareCall(sql);
		ps.execute();
		
		return user;
	}

	@Override
	public void update(Connection ctn,int userId, UserBean nUser) throws SQLException {

	}

	@Override
	public void updateName(Connection ctn,int userId, String name) throws SQLException {

	}

	@Override
	public void updatePassword(Connection ctn,int userId, String password) throws SQLException {

	}

	@Override
	public void updateEmail(Connection ctn,int userId, String email) throws SQLException {

	}

	@Override
	public void updateGender(Connection ctn,int userId, String gender) throws SQLException {
	}

	@Override
	public void updateBirthday(Connection ctn,int userId, Date birthday) throws SQLException {

	}

	@Override
	public void updatePhone(Connection ctn,int userId, String phone) throws SQLException {

	}

	@Override
	public void updateLatestUpdateTime(Connection ctn,int userId, Timestamp lastestLoginTime)
			throws SQLException {

	}

	@Override
	public UserBean get(Connection ctn,int userId) throws SQLException {
		sql = "select * from " + tableName + " where " + idFN + " = " + userId;
		ps = ctn.prepareCall(sql);
		rs = ps.executeQuery();
		
		return wrapRs2Ub(rs);
	}

	@Override
	public List<UserBean> getByName(Connection ctn, String name)
			throws SQLException {
		sql = "select * from " + tableName + " where " + nameFN + " = \"" + name + "\"";
		
		ps = ctn.prepareCall(sql);
		rs = ps.executeQuery();
		
		return wrapRs2UbList(rs);
	}

	@Override
	public List<UserBean> getByEmail(Connection ctn, String email)
			throws SQLException {
		return null;
	}

	@Override
	public List<UserBean> getByGender(Connection ctn, String gender)
			throws SQLException {
		return null;
	}

	@Override
	public List<UserBean> getByBirthday(Connection ctn, Date birthday)
			throws SQLException {
		return null;
	}

	@Override
	public List<UserBean> getByPhone(Connection ctn, String phone)
			throws SQLException {
		return null;
	}
	
	/**
	 * Wraps a record pointed to by the current pointer in the ResultSet into the UserBean
	 * 将ResultSet中当前指针指向的一条记录封装到UserBean中
	 * 
	 * @param rs
	 * @return
	 * @throws SQLException 
	 */
	protected UserBean wrapRs2Ub(ResultSet rs) {

		UserBean user = new UserBean();
		
		try {
			while(rs.next()){
				user.setUserId(rs.getInt(idFN));
				user.setName(rs.getString(nameFN));
				user.setPassword(rs.getString(passwordFN));
				user.setEmail(rs.getString(emailFN));
				user.setGender(rs.getString(genderFN));
				user.setBirthday(rs.getDate(birthdayFN));
				user.setPhone(rs.getString(phoneFN));
				user.setRegisterTime(rs.getTimestamp(registerTimeFN));
				user.setLatestLoginTime(rs.getTimestamp(latestLoginTimeFN));
				break;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return user;
	}
	
	/**
	 * Wraps a record pointed to by the current pointer in the ResultSet into the UserBean list
	 * 将ResultSet中当前指针指向的一条记录封装到UserBean list中
	 * @param rs
	 * @return
	 * @throws SQLException
	 */
	protected List<UserBean> wrapRs2UbList(ResultSet rs) throws SQLException{
		List<UserBean> list = new ArrayList<>();
		UserBean user = null;
		while(rs.next()){
			user = new UserBean();
			user.setUserId(rs.getInt(idFN));
			user.setName(rs.getString(nameFN));
			user.setPassword(rs.getString(passwordFN));
			user.setEmail(rs.getString(emailFN));
			user.setGender(rs.getString(genderFN));
			user.setBirthday(rs.getDate(birthdayFN));
			user.setPhone(rs.getString(phoneFN));
			user.setRegisterTime(rs.getTimestamp(registerTimeFN));
			user.setLatestLoginTime(rs.getTimestamp(latestLoginTimeFN));
			list.add(user);
		}
		return list;
	}

}
